package com.stroke.tools.animation;

import javax.swing.*;
import java.util.Collection;
import java.util.List;
import java.awt.*;

/**
 * Author: Igor Bubelov
 * Date: 3/12/11 11:52 PM
 */
public class FrameList extends JList {
    private FrameListModel model;

    public FrameList() {
        model = new FrameListModel();
        setModel(model);
    }

    @Override
    public FrameListModel getModel() {
        return model;
    }

    public void setData(Collection<Frame> data) {
        model.getElements().clear();
        model.getElements().addAll(data);
        updateUI();
    }

    public void addFrame(Frame frame) {
        model.getElements().add(frame);
        updateUI();
    }

    public void removeFrame(Frame frame) {
        int position = model.getElements().indexOf(frame);

        for (int i = position; i < model.getSize(); i++) {
            model.getElements().get(i).decPosition();
        }

        model.getElements().remove(frame);

        model.sortByPosition();

        updateUI();
    }

    public void moveFrame(int index, int direction) {
        switch (direction) {
            case 1 : {
                if (index != 0) {
                    ((Frame) model.getElementAt(index - 1)).incPosition();
                    ((Frame) model.getElementAt(index)).decPosition();

                    model.sortByPosition();
                    setSelectedIndex(index - 1);
                }

                break;
            }
            case -1 : {
                if (index != model.getSize() - 1) {
                    ((Frame) model.getElementAt(index)).incPosition();
                    ((Frame) model.getElementAt(index + 1)).decPosition();

                    model.sortByPosition();
                    setSelectedIndex(index + 1);
                }

                break;
            }
            default : {
                throw new UnsupportedOperationException();
            }
        }
    }
}
